#pragma once
#include "stdafx.h"
#include <semaphore.h>
#include "Packet.h"
#include "McuHandler.h"
#include "PacketParser.h"
#include "Uart.h"
#include "Lock.h"

class McuManager : public IOnRecv, public IOnPacket
{
private:
	McuManager();
	~McuManager();
public:
	static McuManager* Instance();
	bool Open();
	void Close();
	bool IsOpen();
	virtual void OnRecv(const unsigned char* pBuf, int length);
	virtual void OnPacket(Packet* pPacket);
	Packet* Request(Packet* pPacket, bool wait = true);
private:
	void Notify();
	int  Wait(int timeout);
	Packet* RequestInner(Packet* pPacket, bool wait);
private:
	static McuManager* m_pInstance;
	Uart  m_Uart;
	PacketParser m_Parser;
	McuHandler* m_pHandler;
	Packet* m_pRespPacket;
	int  m_ReqCmd;
	CLock m_Lock;
	sem_t m_hSem;
};
